package gamecomponents.levelcomponents.stagelements.playercontrol;
import gamecomponents.character.Player;
import gamecomponents.levelcomponents.stagelements.Block;
import gamecomponents.navigation.Coordinate;
import gamecomponents.navigation.Direction;
import gamecomponents.navigation.Location;
import gamecomponents.skeletontesthelper.Assistant;
import gamecomponents.skeletontesthelper.Interaction;
import gamecomponents.skeletontesthelper.Parameter;
import javax.swing.Timer;

/**
 * A Physics diszkretizál
 * @author Akos
 * @version 1.0
 * @updated 14-márc.-2012 16:37:40
 */
public class Physics {

	/**
	 * Timer adott szakaszára meghívjuk, a player mozgatását (playerControl)
	 */
	private Timer timer;
	private Player player;
	private Impulse playersImpulse;

	public Physics(){

	}

	public void finalize() throws Throwable {

	}

	/**
	 * 
	 * @param _player
	 */
	public Physics(Player _player){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(_player);
            Assistant.write(Interaction.CRT,this,"Physics",param);
            player = _player;
	}

	/**
	 * 
	 * @param dir
	 */
	public void addForceDirection(Direction dir){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(dir);
            Assistant.write(Interaction.CALL,this,"addForceDirection",param);
            /*
             * A Directiont, hozzáadjuk az Impulse megfelelő komponenséhez
             */
            Impulse imp = new Impulse();
            //ALGORITMUS
            //TODO addForceDirection algoritmus
            /*switch(dir){
                case UP:
                    imp.setY(playersImpulse.getY()+1);
                case DOWN:
                    imp.setY(playersImpulse.getY()-1);
                case RIGHT:
                    imp.setX(playersImpulse.getX()+1);
                case LEFT:
                    imp.setX(playersImpulse.getX()-1);
            }*/
            setPlayersImpulse(imp);
	}

	/**
	 * Ez fogja meghívni egy adott algoritmus alapján kivlaszott Direction
	 * paraméterrel a moveDiscrete-t.
	 */
	public void controlPlayer(){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"controlPlayer",param);
            boolean canFall = player.canIFall();
            Location playersLoc = player.getLocation();
            Block currentBlock = playersLoc.getBlock();
            int x=0, y=0;   //Értékadás, hogy forduljon
            Coordinate nextCoordinate = new Coordinate(x,y);
            Location nextLocation = new Location(nextCoordinate,currentBlock);
            Direction dir = currentBlock.atEdge(nextLocation);
            if(dir!=null){
                Block nextBlock = currentBlock.getNeighbour(dir);
                /*
                 * ALGORITMUS!!!
                 * TODO controlPlayer algoritmus!!
                 * x és y módosul
                 */
                nextCoordinate.setX(x);
                nextCoordinate.setY(y);
                nextLocation.setBlock(nextBlock);
            }
            player.moveToThisLocation(nextLocation);
	}

	/**
	 * 
	 * @param imp
	 */
	public void setPlayersImpulse(Impulse imp){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(imp);
            Assistant.write(Interaction.CALL,this,"setPlayerImpulse",param);
            playersImpulse = imp;
	}

	public void startTimer(){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"startTimer",param);
            timer.start();
	}

	public void stopTimer(){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"stopTimer",param);
            timer.stop();
	}

}